package com.youxin.cpcenter.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.youxin.busfoundation.bean.classify.RequestClassifyIndustryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youxin.busfoundation.bean.classify.RequestCityBean;
import com.youxin.busfoundation.bean.classify.RequestClassifyBean;
import com.youxin.busfoundation.bean.classify.ResponseCityBean;
import com.youxin.busfoundation.bean.classify.ResponseClassifyBean;
import com.youxin.cpcenter.dao.TbConfigIndustryMapper;
import com.youxin.cpcenter.dao.TbConfigRegionNewMapper;
import com.youxin.cpcenter.service.ClassifyService;

@Service("classifyService")
public class ClassifyServiceImpl implements ClassifyService{

	@Autowired
	private TbConfigIndustryMapper tbConfigIndustryMapper;

	@Autowired
	private TbConfigRegionNewMapper tbConfigRegionNewMapper;
	
	public List<ResponseClassifyBean> listTotalIndustrys()
	{
		List<ResponseClassifyBean> totallists=tbConfigIndustryMapper.selectAll();	
		return totallists;
	}

	@Override
	public List<ResponseClassifyBean> listIndustrys(RequestClassifyBean bean) throws CloneNotSupportedException {
		List<ResponseClassifyBean> totallists=listTotalIndustrys();	
		return listIndustrys(totallists);
	}
	
	public List<ResponseClassifyBean> listIndustrys(List<ResponseClassifyBean> totallists) throws CloneNotSupportedException {		
		List<ResponseClassifyBean> oneLevelList=new ArrayList<>(30);
		for(ResponseClassifyBean item:totallists)
		{
			if(item.isTopLevel()){
				oneLevelList.add((ResponseClassifyBean)item.clone());
			}else{
				ResponseClassifyBean parent=item.isChildInItems(oneLevelList);
				if(parent!=null)
				{
					parent.addChild(item);
				}
			}
		}
		return oneLevelList;
	}
	

	@Override
	public List<ResponseClassifyBean> selectByUids(RequestClassifyIndustryBean bean) {
		return tbConfigIndustryMapper.selectByUids(bean);
	}
	
	public List<ResponseCityBean> listTotalCitys()
	{
		List<ResponseCityBean> totallists=tbConfigRegionNewMapper.selectAll();
		return totallists;
	}
	
	public List<ResponseCityBean> listCity(List<ResponseCityBean> totallists) throws CloneNotSupportedException {
		List<ResponseCityBean> oneLevelList=new ArrayList<>(40);
		Map<Integer, ResponseCityBean> cityMaps=new HashMap<>(1000);
		for(ResponseCityBean item:totallists)
		{
			ResponseCityBean cityBean=(ResponseCityBean) item.clone();
			if(item.isTopLevel()){
				oneLevelList.add( cityBean);
				cityMaps.put(cityBean.getRegionId(), cityBean);
			}else{				
				ResponseCityBean parent=cityMaps.get(item.getParentId());
				if(parent!=null&&parent.isTopLevel())
				{
					parent.addChild(item);
				}
				cityMaps.put(cityBean.getRegionId(), cityBean);
			}
		}
		return oneLevelList;
	}
	
	/**
	 * 区域列表
	 */
	@Override
	public List<ResponseCityBean> listCity(RequestCityBean bean) throws CloneNotSupportedException {
		List<ResponseCityBean> totallists=listTotalCitys();
				//tbConfigRegionMapper.selectAll();
		return listCity(totallists);
	}

}
